/*  
 * @(#) DateHelper.java Create on 2013-10-10 下午03:07:31   
 *   
 * Copyright 2013 by san.   
 */


package com.springdemo.tools;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 日期工具类
 * @author ss
 * @date   2013-10-10
 */
public class DateHelper {
	private String minDate;
	private String maxDate;
	
	public String getMaxDate() {
		return maxDate;
	}

	public String getMinDate() {
		return minDate;
	}

	public static String formatDateToStr(int date){
		SimpleDateFormat simpleDateFormat4Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat4Time.format(date * 1000);
	}
	public void setDateType(int type) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy");
		maxDate = simpleDateFormat.format(today).toString();
		if(type==1){
			//3天内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(3-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==2){
			//一周内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(7-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==3){
			//一月内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(30-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==4){
			//三月内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(90-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}
		this.maxDate = maxDate;
		this.minDate = mixDate;
		//return simpleDateFormat.format(date);
	}
	public void setDateType(int type,String datetype) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(datetype);
		maxDate = simpleDateFormat.format(today).toString();
		if(type==1){
			//3天内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(3-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==2){
			//一周内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(7-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==3){
			//一月内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(30-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==4){
			//三月内
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(90-1)));
			mixDate = simpleDateFormat.format(date).toString();
		}else if(type==0){
			date = new Date(System.currentTimeMillis()-(0x5265c00L*(0-1)));
			maxDate = simpleDateFormat.format(date).toString();
			mixDate = simpleDateFormat.format(today).toString();
		}
		this.maxDate = maxDate;
		this.minDate = mixDate;
	}
	
	
	public static String getOneYearAgoTime(String format,int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, days);
		Date date = calendar.getTime();
		SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
		return simpledateformat.format(date);
    }
	
	/**
	 * 获取距离当前时间 days 天的时间
	 * @Title: setDate
	 * @data:2014-2-18上午9:58:50
	 * @author:san
	 *
	 * @param days
	 * @param datetype
	 */
	public void setDate(int days,String datetype) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(datetype);
		maxDate = simpleDateFormat.format(today).toString();
		date = new Date(System.currentTimeMillis()-(0x5265c00L*days));
		mixDate = simpleDateFormat.format(date).toString();
		this.maxDate = maxDate;
		this.minDate = mixDate;
	}
	
	
	/**
	 * 改变日志格式
	 * @param datetime
	 * @param oldFormat
	 * @param newFormat
	 * @return
	 */
	public static String formatDateString(String datetime,String oldFormat,String newFormat){
		Date date = null;
		try {
			if(datetime==null||datetime.equals("")){
				return "";
			}
			SimpleDateFormat formatter = new SimpleDateFormat(oldFormat);
			date=formatter.parse(datetime);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return getSpecifiedDate(date.getTime(), newFormat);
	}
	/**
	 * 根据指定的格式显示时间
	 * 
	 * @param time
	 *            long
	 * @param format
	 *            String
	 * author xieyan           
	 * @return String
	 */
	public static String getSpecifiedDate(long time, String format) {
		if (time == 0) {
			return "-";
		}

		Date timeVal = new Date(time);
		
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
			strTime = simpledateformat.format(timeVal);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strTime;
	}
	
	/**
	 * 转化时间格式
	 * 
	 * @param time
	 *            long
	 * @param format
	 *            String
	 * author xieyan           
	 * @return String
	 */
	public static Date getOrsaToJavaDate(Date time, String format) {

		if(time==null){
			return time;
		}
		Date date = null ;
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
			strTime = simpledateformat.format(time);
			if(strTime==null || strTime==""){
				return date;
			}
			date = simpledateformat.parse(strTime);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return date;
	}
	
	
	/**
	 * 格式 ： 输出指定格式类型的时间
	 * 
	 * @param formater
	 *            String
	 * author xieyan           
	 * @return String
	 */
	public static String getNowDate(String formater) {
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(formater);
			strTime = simpledateformat.format(new Date());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strTime;
	}
	public static void main(String args[]){
		String dateTime = "2015-3-1";
		String aa = "";
		try {
			aa = addDate(dateTime,-1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println(aa);
		
	}
	
	/**
	 * string转换成long时间
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static long formatStringToLong(String dateString,String format){
		long result = 0l;
		SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
		try {
			 result = simpledateformat.parse(dateString).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return  result;
	}
	
	
	/**
	 * 获取 后一天的日期
	 * @Title: addDate
	 * @data:2014-2-18上午9:57:02
	 * @author:san
	 *
	 * @param strDate
	 * @param days
	 * @return
	 * @throws ParseException
	 */
	 public static String addDate(String strDate, int days) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date cDate = df.parse(strDate);
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(cDate);
		gcalendar.add(Calendar.DATE, days);
		return df.format(gcalendar.getTime()).toString();
	}
	 
	 
	 
	 /**
	  * 传递当前天，获得最后的时间，例如 2014-2-18 传进去，获取的时2014-2-18 23：59：59
	  * @Title: getDayEndSecond
	  * @data:2014-2-18上午9:56:01
	  * @author:san
	  *
	  * @param dateTime
	  * @return
	  */
	 public static String getDayEndSecond(String dateTime){
		 //String result = "";
		 if(!dateTime.equals("")){
			 try {
				 dateTime = DateHelper.formatDateString(dateTime+" 23:59:59", DateHelper.FMT_DATE_DATETIME, FMT_DATE_DATETIME);
			} catch (Exception e) {
			}
		 }
		 
		 return dateTime;
	 }
	 
	 /**
	  * 获得timestamp格式类型的时间
	  * @Title: getTimestampNowDate
	  * @data:2014-2-18上午9:51:55
	  * @author:san
	  *
	  * @return
	  */
	public static Timestamp getTimestampNowDate() {
		Timestamp timestamp = null;
		try {
			timestamp = new Timestamp(new Date().getTime());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return timestamp;
	}
	
	/**
	 * 得到当前时间 精确到 分
	 * @Title: getCurrentTime
	 * @data:2014-2-18上午9:51:10
	 * @author:san
	 *
	 * @return
	 */
	public static String getCurrentTime(){
		return getNowDate(FMT_DATE_DATETIME);
	}
	/**
	 * 得到当前天
	 * @Title: getCurrentDate
	 * @data:2014-2-18上午9:51:35
	 * @author:san
	 *
	 * @return
	 */
	public static String getCurrentDate(){
		return getNowDate(FMT_DATE_YYYY_MM_DD);
	}
	
	public static String getTimeFromDatebase(String time){
		if(time!=null && time.length() > 0){
			return formatDateString(time, FMT_DATE_DATETIME, FMT_DATE_DATETIME);
		}
		return "";
	}
	
	/**
	 * 得到当天yyyy-MM-dd hh:ss:mm 返回Timestamp类型
	 * @Title: currentTime_Timestamp
	 * @data:2014-4-14下午4:42:22
	 * @author:qq
	 *
	 * @return
	 */
	public static Timestamp currentTime_Timestamp(){
		Date datetime=new Date();
		Timestamp time=new Timestamp(datetime.getTime());
		return time;
	}

	/**
	 * 返回sql date(不带时分秒)yyyy-MM-dd
	 * @Title: currentTime_sqlDate
	 * @data:2014-4-17下午1:32:57
	 * @author:qq
	 *
	 * @return
	 */
	public static java.sql.Date currentTime_sqlDate(){
		Date datetime=new Date();
		return new java.sql.Date(datetime.getTime());
	}
	
	  /**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */
	 public static int daysBetween(String smdate,String bdate) throws ParseException{  
	        SimpleDateFormat sdf=new SimpleDateFormat(FMT_DATE_YYYY_MM_DD);  
	        Calendar cal = Calendar.getInstance();    
	        cal.setTime(sdf.parse(smdate));    
	        long time1 = cal.getTimeInMillis();                 
	        cal.setTime(sdf.parse(bdate));    
	        long time2 = cal.getTimeInMillis();         
	        long between_days=(time2-time1)/(1000*3600*24);  
	            
	       return Integer.parseInt(String.valueOf(between_days));     
	    }
	 
	 /**
	  * 判断日期格式是否正确
	  * @Title: isDate
	  * @data:2017年5月11日下午1:42:57
	  * @author:lysk1
	  *
	  * @param date  要校验的日期
	  * @param format  校验日期格式
	  * @return true  符合，FALSE 不符合
	  */
	 public static boolean isDate(String date,String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		boolean result = false;
		try {
			sdf.parse(date);
			result = true;
		} catch (Exception e) {
			result = false;
		}
		return result;
	}
	 
	 /**
	  * 获取两个日期之间的日期(包含开始和结束日期)
	  * @param start 开始日期
	  * @param end 结束日期
	  * @return 日期集合
	  */
	 public static List<Date> getBetweenDates(Date start, Date end) {
	     List<Date> result = new ArrayList<Date>();
	     Calendar tempStart = Calendar.getInstance();
	     tempStart.setTime(start);
	   //  tempStart.add(Calendar.DAY_OF_YEAR, 1);
	     
	     Calendar tempEnd = Calendar.getInstance();
	     tempEnd.setTime(end);
	     while (tempStart.before(tempEnd)) {
	         result.add(tempStart.getTime());
	         tempStart.add(Calendar.DAY_OF_YEAR, 1);
	     }
	     result.add(end);
	     return result;
	 }
	 
    public static final String FMT_DATE_YYYY = "yyyy";
    public static final String FMT_DATE_YYMMDD = "yyMMdd";
    public static final String FMT_DATE_YYYYMMDD = "yyyyMMdd";
    public static final String FMT_DATE_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String FMT_DATE_YYYY_MM_DD2 = "yyyy/MM/dd";
    public static final String FMT_DATE_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FMT_DATE_DATETIME3 = "yyyy-MM-dd HH:mm:sss";
    public static final String FMT_DATE_DATETIME2 = "yyyy-MM-dd HH:mm";
    public static final String FMT_DATE_DATETIME_TIGHT = "yyyyMMddHHmmss";
    public static final String FMT_DATE_YY_MM = "yy-MM";
    public static final String FMT_DATE_MM = "MM";
    public static final String FMT_DATE_DD = "dd";
    public static final String FMT_DATE_TIME = "HH:mm:ss";
	public static final String FMT_DATE_DATEMONTH = "yyyy-MM";

}
